<?php
class aluImportTask extends sfBaseTask
{

  protected function configure()
  {
    $this->namespace = 'alu';
    $this->name      = 'import';
    $this->addArgument('file', sfCommandArgument::REQUIRED, 'file path');
    $this->addOption('deconnexion', null, sfCommandOption::PARAMETER_NONE);
  }
  protected function execute($arguments = array(), $options = array())
  {
    sfContext::createInstance($this->createConfiguration('frontend', 'prod'));

    if (!file_exists($arguments['file']))
    {
      throw new sfException('Fichier non trouvé');
    }

    $tab = file($arguments['file']);
    $tab = array_map('trim', $tab);

    foreach ($tab as $ligne)
    {
      //TODO validateurs
      $date    = substr($ligne, 0, 17);
      $tmp     = explode(' ', $ligne);
      $user    = array_pop($tmp);
      $machine = array_pop($tmp);

      $jour    = substr($date, 0, 2);
      $mois    = substr($date, 3, 2);
      $annee   = substr($date, 6, 4);
      $heure   = substr($date, 11, 2);
      $minute  = substr($date, 14, 2);
      $seconde = substr($date, 17, 2);

      $timestamp = mktime($heure, $minute, $seconde, $mois, $jour, $annee);

      $type = ($options['deconnexion']) ? TypesPeer::TYPE_DECONNEXION : TypesPeer::TYPE_CONNEXION;

      $log = new Log();
      $log->setDate($timestamp);
      $log->setMachine($machine);
      $log->setUtilisateur($user);
      $log->setType($type);
      $log->save();
    }

    $oType = TypesPeer::retrieveByPK($type);
    $this->logSection('Import en tant que ' . $oType->getLibelle(), $arguments['file']);
  }
}
